<template>
    <div class="User_page">
        <!-- //面包屑导航 -->
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>用户管理</el-breadcrumb-item>
            <el-breadcrumb-item>用户列表</el-breadcrumb-item>
        </el-breadcrumb>
        <!-- 内容卡片 -->
        <el-card class="box-card">
            <!-- 搜索区域 -->
            <el-row :gutter="20">
                <el-col :span="12">
                <el-input placeholder="请输入内容"  clearable>
                    <el-button slot="append" icon="el-icon-search" ></el-button>
                </el-input>
                </el-col>
                <el-col :span="4">
                <el-button type="primary" @click="addUserBtn">添加用户</el-button>
                </el-col>
            </el-row>
            <!-- 管理员信息表格 -->
            <el-table stripe border :data="usersInfo" style="width: 100%">
                 <el-table-column type="index" label="#" width="50">
                </el-table-column>
                <el-table-column prop="username" label="姓名">
                </el-table-column>
                <el-table-column prop="email" label="邮箱">
                </el-table-column>
                <el-table-column prop="mobile" label="电话">
                </el-table-column>
                <el-table-column prop="role_name" label="角色">
                </el-table-column>
                <el-table-column prop="mg_state" label="状态">
                    <template slot-scope="scope">
                        <el-switch v-model="scope.row.mg_state" active-color="#13ce66" inactive-color="#ddd" @change="status(scope.row)"></el-switch>
                    </template>
                </el-table-column>
                <el-table-column label="操作">
                    <template slot-scope="scope">
                        <el-button type="primary" icon="el-icon-edit" size="mini" @click="editUser(scope.row)"></el-button>
                        <el-button type="danger" icon="el-icon-delete" size="mini" @click="delUser(scope.row)"></el-button>
                        <el-button type="warning" icon="el-icon-setting" size="mini" @click="changeUser(scope.row)"></el-button>
                    </template>
                </el-table-column>
            </el-table>
            <!-- 分页列表 -->
            <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="queryParams.pagenum"
                :page-sizes="[1, 2, 5, 10]"
                :page-size="queryParams.pagesize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total">
            </el-pagination>

            <!-- 添加用户弹出框 -->
            <el-dialog title="添加用户" :visible.sync="dialogVisible" width="65%" :before-close="handleClose">
                <el-form :model="ruleForm" :rules="rules"  ref="ruleForm" label-width="70px">
                    <el-form-item label="用户名" prop="username">
                        <el-input v-model="ruleForm.username"></el-input>
                    </el-form-item>
                     <el-form-item label="密码" prop="password">
                        <el-input v-model="ruleForm.password"></el-input>
                    </el-form-item>
                     <el-form-item label="邮箱" prop="email">
                        <el-input v-model="ruleForm.email"></el-input>
                    </el-form-item>
                     <el-form-item label="手机号" prop="mobile">
                        <el-input v-model="ruleForm.mobile"></el-input>
                    </el-form-item>
                </el-form>
                <span slot="footer" class="dialog-footer">
                    <el-button @click="handleClose">取 消</el-button>
                    <el-button type="primary" @click="addUser">确 定</el-button>
                </span>
            </el-dialog>
            <!-- 编辑用户弹出框 -->
            <el-dialog title="添加用户" :visible.sync="editdialogVisible" width="65%" :before-close="editClose">
                <el-form :model="editForm" :rules="rules"  ref="editForm" label-width="70px">
                    <el-form-item label="用户名">
                        <el-input disabled v-model="editForm.username"></el-input>
                    </el-form-item>
                     <el-form-item label="邮箱" prop="email">
                        <el-input v-model="editForm.email"></el-input>
                    </el-form-item>
                     <el-form-item label="手机号" prop="mobile">
                        <el-input v-model="editForm.mobile"></el-input>
                    </el-form-item>
                </el-form>
                <span slot="footer" class="dialog-footer">
                    <el-button @click="editClose">取 消</el-button>
                    <el-button type="primary" @click="upDataUserInfo">确 定</el-button>
                </span>
            </el-dialog>
        </el-card>
    </div>
</template>

<script>
    export default {
        data(){
            // 验证邮箱的正则表达式
            var checkEmail = (rule,val,cb)=>{
                // 验证邮箱的正则表达式
                const regEmail = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/
                if(regEmail.test(val)){
                    return cb()
                }
                cb('请输入正确的邮箱~')
            }
            var checkMobile = (rule,val,cb)=>{
                //验证手机号的正则表达式
                const regMobile = /^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/
                
                if(regMobile.test(val)){
                    return cb()
                }
                cb('请输入正确的手机号~')
            }
            return {
                queryParams:{
                    query:'',
                    pagesize:2,
                    pagenum:1
                },
                usersInfo:[],
                total:0,
                dialogVisible:false,
                editdialogVisible:false,
                ruleForm:{
                    username:'',
                    password:'',
                    email:'',
                    mobile:''
                },
                rules:{
                    username: [
                        { required: true, message: '请输入用户名称', trigger: 'blur' },
                        { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
                    ],
                    password: [
                        { required: true, message: '请输入密码', trigger: 'blur' },
                        { min: 6, max: 16, message: '长度在 6 到 16 个字符', trigger: 'blur' }
                    ],
                    email: [
                        { required: true, message: '请输入邮箱', trigger: 'blur' },
                        { validator:checkEmail, trigger: 'blur' }
                    ],
                    mobile: [
                        { required: true, message: '请输入手机号', trigger: 'blur' },
                        { validator:checkMobile, trigger: 'blur' }
                    ]
                },
                editForm:{
                },
                editInfo:{
                    emdil:'',
                    mobile:''
                }
            }
        },
        created(){
           this.getUserInfo()
        },
        methods:{
            // 获取用户列表信息的事件处理函数
            async getUserInfo(){
                 const {data:res} = await this.$http.get('users',{params:this.queryParams})
                 if(res.meta.status != 200) return this.$message.error(res.meta.msg)
                 this.usersInfo = res.data.users
                 this.total = res.data.total
            },
            handleSizeChange(val) {
                this.queryParams.pagesize = val
                this.getUserInfo()
            },
            handleCurrentChange(val) {
                this.queryParams.pagenum = val
                this.getUserInfo()
            },
            // 修改用户状态事件处理
            async status(data){
                const {data:res} = await this.$http.put("users/"+data.id+"/state/"+data.mg_state)
                if(res.meta.status !== 200) {
                    data.mg_state = !data.mg_state
                    return this.$message.error(res.meta.msg)
                }
                this.$message.success('用户状态修改成功~')
            },
            // 点击编辑按钮的事件
            editUser(data){
                this.editForm = data
                this.editInfo.email = data.email
                this.editInfo.mobile = data.mobile
                this.editdialogVisible = true

            },
            //删除u按钮的点击事件
            async delUser(data){
                const res = await this.$confirm('此操作将永久删除该用户, 是否继续?', '温馨提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                    }).catch(()=>{})
                    if(res !== 'confirm') return this.$message.info('已取消删除~')
                const {data:del} = await this.$http.delete('/users/'+data.id)
                   if(del.meta.status !== 200) return this.$message.warning('删除用户失败~')
                   this.$message.success('恭喜你,成功干掉了一个用户~')
                   this.getUserInfo()
            },
            changeUser(data){

            },
            //添加用户信息的按钮的事件
            addUserBtn(){
                this.dialogVisible = true
            },
            //添加用户信息页面关闭的事件
            handleClose(){
                this.dialogVisible = false
                this.$refs.ruleForm.resetFields()
            },
            //编辑用户信息页面关闭的事件
            editClose(){
                this.editdialogVisible = false
                this.$refs.editForm.resetFields()
            },
            // 修改用户信息页面确认按钮的处理事件
            upDataUserInfo(){
                if(this.editInfo.email == this.editForm.email && this.editInfo.mobile == this.editForm.mobile){
                    return this.$message.error('您还没有修改什么信息哦~')  
                }
                this.$refs.editForm.validate(async (valid)=>{
                    if(!valid)return this.$message.error('请输入正确的信息哦~')
                    const {data:result} = await this.$http.put('/users/'+this.editForm.id,{email:this.editForm.email,mobile:this.editForm.mobile})
                    if(result.meta.status !== 200) return this.$message.error(result.meta.msg)
                    this.$message.success('用户信息修改成功~')
                    this.getUserInfo()
                    this.editdialogVisible = false
                })
            },
            // 添加页面的确认添加按钮的添加用户事件
            addUser(){
                this.$refs.ruleForm.validate(async (valid)=>{
                    if(!valid) return this.$message.error('请填写正确的表单信息~')
                    const {data:res} = await this.$http.post('/users',this.ruleForm)
                    if(res.meta.status !== 201) return this.$message.error(res.meta.msg)
                    this.$message.success('添加新用户成功~')
                    this.dialogVisible = false
                    this.getUserInfo()
                })
            }
        }
    }
</script>

<style scoped>
.el-table {
    margin-top: 20px;
}
.el-pagination {
    margin-top: 10px;
}
</style>